package userInterface;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import players.PlayerLevel;

import staticClasses.Global;
import staticClasses.Initialization;
import staticClasses.PlayerManager;

public class SimulationFrame {
		
	private SettingsPanel stp;
	private JPanel simulationSettingsPanel;
	private JPanel fieldPanel;
	private JTabbedPane fieldSectionPanel;
	private JPanel controlGamePanel;
	private JButton playGameButton;
	
	
	//simulation frame of the application
	public void go() {
		
		//Create the frame
		JFrame simulationFrame = new JFrame();
		simulationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		simulationFrame.setResizable(false);
		simulationFrame.setTitle("Soccer game simulation");
		
		//Menu Bar
		JMenuBar menuSimulation = new JMenuBar();
		JMenu file = new JMenu("File");
	    JMenuItem playMenuItem = new JMenuItem("Play game");
	    playMenuItem.addActionListener(new PlayListener());
	    file.add(playMenuItem);
	    JMenuItem quitMenuitem = new JMenuItem("Quit");
	    quitMenuitem.addActionListener(new QuitListener());
	    file.add(quitMenuitem);   
	    JMenu help = new JMenu("Help");
	    JMenuItem helpMenuItem = new JMenuItem("Read me");
	    helpMenuItem.addActionListener(new HelpListener());
	    help.add(helpMenuItem);
	    menuSimulation.add(file);
	    menuSimulation.add(help);
	     
	    //Integrate the Simulation settings panel into the frame
	    stp = new SettingsPanel();
	    simulationSettingsPanel = stp.create();
	    JPanel buttonPanel = (JPanel) simulationSettingsPanel.getComponent(2);
	    JButton playButton = (JButton) buttonPanel.getComponent(1);
	    simulationFrame.getRootPane().setDefaultButton(playButton);
	    playButton.addActionListener(new PlayListener());
	    
	    Border raisedbevel, loweredbevel,compound;
	    raisedbevel = BorderFactory.createRaisedBevelBorder();
	    loweredbevel = BorderFactory.createLoweredBevelBorder();
	    Border blueline = BorderFactory.createLineBorder(new Color(39, 47, 120));
	    compound = BorderFactory.createCompoundBorder(raisedbevel, loweredbevel);
	    compound = BorderFactory.createCompoundBorder(blueline, compound);
		simulationSettingsPanel.setBorder(BorderFactory.createTitledBorder(compound));
		
		JScrollPane scrollPaneSimulationSettings = new JScrollPane(simulationSettingsPanel);
		simulationSettingsPanel.setPreferredSize(new Dimension(310, 500)); //410, 955
		scrollPaneSimulationSettings.setPreferredSize(new Dimension(330, 500)); //430, 700
		
		//Field, information and control section frame
	    //Field section
	    fieldSectionPanel = new JTabbedPane();
	    fieldPanel = new JPanel();
	    JLabel noSimulationLabel = new JLabel();
		String noSimulationString = "<html> " +
										"<p align=\"center\"><font size=\"4\" color=\"##585858\"><b>No game for the moment</font></b></p><br /> " +
										"<p align=\"center\"><font size=\"4\" color=\"##585858\">Please complete all the parameters and click on 'Play game' to start the game</font></p><br /> " +
									"</html>";
		noSimulationLabel.setText(noSimulationString);
		fieldPanel.add(noSimulationLabel);
		fieldSectionPanel.add("Soccer Field", fieldPanel);
		
		
		//information section
		JLabel scoreLabel = new JLabel("<html> " +
											"<p align=\"center\"><font size=\"5\" color=\"#272F78\"><b>Display Settings</font></b></p>" +
										"</html>");
		
		JPanel infoPanel = new JPanel();
		infoPanel.add(scoreLabel);
		
		//Control buttons section
		playGameButton = new JButton("pause");
		playGameButton.addActionListener(new PlayPauseListener());
		JButton stopGameButton = new JButton("stop");
		stopGameButton.addActionListener(new StopListener());
		JSlider gameSpeedSlider = new JSlider(1, 10, 5);
		gameSpeedSlider.setMajorTickSpacing(1);
		gameSpeedSlider.setMinorTickSpacing(1);
		gameSpeedSlider.setPaintTicks(true);
		gameSpeedSlider.setPaintLabels(true);
		gameSpeedSlider.addChangeListener(new SpeedSliderListener());
		controlGamePanel = new JPanel();
		JLabel gameSpeedLabel = new JLabel("speed :");
		controlGamePanel.add(stopGameButton);
		controlGamePanel.add(playGameButton);
		controlGamePanel.add(gameSpeedLabel);
		controlGamePanel.add(gameSpeedSlider);
		for (int i = 0; i < 4; i++) {
			controlGamePanel.getComponent(i).setEnabled(false);
		}
		
		JPanel infoAndControlPanel = new JPanel();
		infoAndControlPanel.setLayout(new BorderLayout());
		infoAndControlPanel.add(BorderLayout.CENTER, infoPanel);
		infoAndControlPanel.add(BorderLayout.SOUTH, controlGamePanel);
		
		//place the different parts in the game simulation panel 
		JPanel simulationPanel = new JPanel();
		simulationPanel.setLayout(new BorderLayout());
		simulationPanel.add(BorderLayout.CENTER, fieldSectionPanel);
		simulationPanel.add(BorderLayout.SOUTH, infoAndControlPanel);
		        
	    //place the different parts in the correct location of the main panel
	    simulationFrame.getContentPane().add(BorderLayout.WEST, scrollPaneSimulationSettings);
	    simulationFrame.getContentPane().add(BorderLayout.CENTER, simulationPanel);

	    simulationFrame.setJMenuBar(menuSimulation);
		simulationFrame.setSize(1310, 760);
		//simulationFrame.pack();
		simulationFrame.setVisible(true);
	
	}
	    
	// ACTION PERFORMED //
	
	//action performed when you select play game
	class PlayListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			try {   
				
				Global.play = true;
				
				//initialization
				Initialization.InitMap();
				Initialization.InitBall();
				Initialization.InitLocalTeam();
				Initialization.InitVisitorTeam();
				
				switch (Global.team1Level) {
				case 1:
					Initialization.InitLevelTeam(Global.localTeam, PlayerLevel.EASY);
					break;
				case 2:
					Initialization.InitLevelTeam(Global.localTeam, PlayerLevel.MEDIUM);
					break;
				case 3:
					Initialization.InitLevelTeam(Global.localTeam, PlayerLevel.HARD);
				default:
					Initialization.InitLevelTeam(Global.localTeam, PlayerLevel.EASY);
					break;
				}
				
				switch (Global.team2Level) {
				case 1:
					Initialization.InitLevelTeam(Global.visitorTeam, PlayerLevel.EASY);
					break;
				case 2:
					Initialization.InitLevelTeam(Global.visitorTeam, PlayerLevel.MEDIUM);
					break;
				case 3:
					Initialization.InitLevelTeam(Global.visitorTeam, PlayerLevel.HARD);
				default:
					Initialization.InitLevelTeam(Global.visitorTeam, PlayerLevel.EASY);
					break;
				}
				
				Global.team2Level = 0;
				Global.team2Level = 0;
				
				//start the manager
				PlayerManager playerManager = new PlayerManager();
				playerManager.start();
	
				//paint the simulation
				fieldSectionPanel.removeAll();
				SimulationThread simulationThread = new SimulationThread(fieldSectionPanel);
				simulationThread.start();
				
				//enable the control game options
				for (int i = 0; i < 4; i++) {
					controlGamePanel.getComponent(i).setEnabled(true);
				}
				
				//disable all the simulation parameters during the game
				JPanel panel1 = (JPanel) simulationSettingsPanel.getComponent(1);
				JPanel panel10 = (JPanel) panel1.getComponent(0);
				for (int i = 0; i < 4; i++) {
					panel10.getComponent(i).setEnabled(false);
				}
				JPanel panel11 = (JPanel) panel1.getComponent(1);
				for (int i = 0; i < 4; i++) {
					panel11.getComponent(i).setEnabled(false);
				}
				JPanel panel12 = (JPanel) panel1.getComponent(2);
				for (int i = 0; i < 2; i++) {
					panel12.getComponent(i).setEnabled(false);
				}
				JPanel panel13 = (JPanel) panel1.getComponent(3);
				panel13.getComponent(0).setEnabled(false);
				JPanel panel2 = (JPanel) simulationSettingsPanel.getComponent(2);
				panel2.getComponent(1).setEnabled(false);	
				
	    	//If the text fields are incorrectly fill in, open a warning window
			} catch (Exception e) {e.printStackTrace();
			}
		}
  	}
	
	//action performed when you select play game
	class PlayPauseListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			try { 
				String textButton = (String)event.getActionCommand();
				if (textButton.equals("pause")) {
					playGameButton.setText("play");
					Global.pause = true;
				} else {
					playGameButton.setText("pause");
					Global.pause = false;
				}
			} catch (Exception e) {
			}
		}
  	}
    
	//action performed when you click on stop
	class StopListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			try {   
				
				//stop the thread of the simulation
				Global.play = false;
				fieldSectionPanel.removeAll();
				fieldSectionPanel.add("Soccer Field", fieldPanel);
				
				//enable all the simulation parameters over new
				JPanel panel1 = (JPanel) simulationSettingsPanel.getComponent(1);
				JPanel panel10 = (JPanel) panel1.getComponent(0);
				for (int i = 0; i < 4; i++) {
					panel10.getComponent(i).setEnabled(true);
				}
				JPanel panel11 = (JPanel) panel1.getComponent(1);
				for (int i = 0; i < 4; i++) {
					panel11.getComponent(i).setEnabled(true);
				}
				JPanel panel12 = (JPanel) panel1.getComponent(2);
				for (int i = 0; i < 2; i++) {
					panel12.getComponent(i).setEnabled(true);
				}
				JPanel panel13 = (JPanel) panel1.getComponent(3);
				panel13.getComponent(0).setEnabled(true);
				JPanel panel2 = (JPanel) simulationSettingsPanel.getComponent(2);
				panel2.getComponent(1).setEnabled(true);	
				
				//disable the control game options
				for (int i = 0; i < 4; i++) {
					controlGamePanel.getComponent(i).setEnabled(false);
				}
				
	    	//If the text fields are incorrectly fill in, open a warning window
			} catch (Exception e) {
			}
		}
  	}
	
	//action performed when you slide the speed slider
    class SpeedSliderListener implements ChangeListener {
        public void stateChanged(ChangeEvent e) {
            JSlider sourceSlider = (JSlider)e.getSource();
	        int value = (int)sourceSlider.getValue();
	        Global.tick = Global.simulationSpeedPossibilities[value-1];
        }
    }
    
    //action performed when you select help in the menu bar
  	class HelpListener implements ActionListener {
  		public void actionPerformed(ActionEvent event) {
  			
  		}
  	}
		
	//action performed when you click on or select quit
	class QuitListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			System.exit(0);
		}
	}
	
}
